class Solution {
private:
    void ro(vector<vector<int>>& matrix, int i, int j) {
        if (i >= j) return;
        vector<int> temp1(j - i + 1, 0);
        vector<int> temp2(j - i + 1, 0);
        for (int t = i; t <= j; t++) {
            temp1[t - i] = matrix[t][j];
            matrix[t][j] = matrix[i][t];
        }
        for (int t = i; t <= j; t++) {
            temp2[t - i] = matrix[j][t];
            matrix[j][j - t] = temp1[t - i];
        }
        for (int t = i; t <= j; t++) {
            temp1[t - i] = matrix[t][i];
            matrix[t][i] = temp2[t - i];
        }
        for (int t = i; t <= j; t++) {
            matrix[i][t] = temp1[i + j - t];
        }
        ro(matrix, i + 1, j - 1);
    }
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        if (n == 1) return;
        ro(matrix, 0, n - 1);
    }
};